let AudioManager = require('AudioManager')
let Main = require('Main')
let GameDataManager = require('GameDataManager')
let WX = require('WX')

cc.Class({
  extends: cc.Component,

  properties: {
    Money: cc.Prefab,
    Ribbon: cc.Prefab
  },

  onLoad () {
    console.log('CashLine-onLoad')
  },

  collisionExplosion (exp, car) {
    // console.log("CashLine-collisionExplosion");
    let carX = car.x

    let carY = car.y
    let _car = car.getComponent('Car')

    AudioManager.instance.play('explosion')

    let cashLineLongContent = this.node
    let cashLineLong = cashLineLongContent.getChildByName('cashLineLong')
    cashLineLong.opacity = 0
    cashLineLong.runAction(cc.sequence(cc.fadeTo(0.15, 255), cc.fadeTo(0.2, 0)))

    let money = cc.instantiate(this.Money)
    money.getComponent(cc.Label).string = exp
    let down1 = cc.moveBy(0.4, 0, -100)
    let down2 = cc.moveBy(0.1, 0, -25)
    let up1 = cc.moveBy(0.1, 0, 25)
    down1.easing(cc.easeIn(3.0))
    down2.easing(cc.easeIn(3.0))
    up1.easing(cc.easeOut(3.0))
    Main.instance.node.addChild(money)
    money.y = carY + 100
    money.x = carX
    money.runAction(
      cc.sequence(
        down1,
        up1,
        down2,
        cc.callFunc(() => {
          money.active = false
          money.removeFromParent()
        })
      )
    )

    let ribbon = cc.instantiate(this.Ribbon)
    ribbon.active = true
    Main.instance.node.addChild(ribbon)
    ribbon.x = carX + 0.4 * _car.vx
    ribbon.y = carY + 0.4 * _car.vy
    ribbon.getComponent(cc.ParticleSystem).autoRemoveOnFinish = true
    setTimeout(() => {
      ribbon.getComponent(cc.ParticleSystem).stopSystem()
    }, 500)
    setTimeout(() => {
      ribbon.removeFromParent()
    }, 1500)
  },

  start () {},

  onCollisionEnter (other, self) {
    if (!Main.instance.isGameStart) return
    let car = other.node
    if (self.node._code === car._code) {
      WX.vibrate(false)

      if (Main.instance.firstPass) {
        Main.instance.firstPass = false
        Main.instance.cashDollar.active = true
      }
      let multi = GameDataManager.setting.debug ? GameDataManager.setting.multi : 1
      console.log(multi);
      let value = GameDataManager.getCarValue(car.carLevel)
      this.collisionExplosion(multi * value, car)
      Main.instance.updateChapterLine(multi * value)
    }
  }

  // update (dt) {},
})
